package com.company.ljh.easy;
/**
 * 557.给定一个字符串 s ，你需要反转字符串中每个单词的字符顺序，同时仍保留空格和单词的初始顺序。
 */

/**
 * @description:
 * @projectName:leet_code
 * @see:com.company.ljh.easy
 * @author:ljh
 * @createTime:2022/6/1 15:14
 * @version:1.0
 */
public class 反转字符串中的单词III {
    public String reverseWords(String s) {
        int start =0;
        int end = 0;
        char []arrays = s.toCharArray();
        for(int i = 0;i<arrays.length;i++){
            if(arrays[i] == ' '){
                end = i -1;
                reverseArrays(arrays,start,end);
                start = i+1;
            }
        }

        //将最后一个单词反转
        end = arrays.length-1;
        reverseArrays(arrays,start,end);
        String str = String.valueOf(arrays);
        return str;
    }
    private void reverseArrays(char[]arrays,int start,int end){
        while(start < end){
            char temp = arrays[start];
            arrays[start] = arrays[end];
            arrays[end] = temp;
            start++;
            end--;
        }
    }
}
